﻿using System;
using System.Linq;
using System.Windows.Forms;

namespace LibraryMangementSystem.BookManage
{
    public partial class PublishUpdate : Form
    {
        private string publishId;
        LibraryEntities1 library = new LibraryEntities1();
        Publisherlnfo publisher;
        public PublishUpdate()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }
        public PublishUpdate(string publishId)
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            this.publishId = publishId;
            publisher= library.Publisherlnfo.FirstOrDefault<Publisherlnfo>(P => P.PublisherId == publishId);

        }
        private void PushValue(object sender, EventArgs e) {
            textBoxPublishId.Text = publisher.PublisherId.Trim();
            textBoxPublish.Text = publisher.Publisher.Trim();
            textBoxAddress.Text = publisher.Address.Trim();
            textBoxTel.Text = publisher.Tel.Trim();
            textBoxFax.Text = publisher.Fax.Trim();
            textBoxPostcode.Text = publisher.Postcode.ToString().Trim();
            textBoxhttp.Text = publisher.http.Trim();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBoxPublish.Text != "" && textBoxPostcode != null)
            {
                

                if (textBoxPublishId.Text.Length > 4)
                {
                    MessageBox.Show("ID过长");
                    return;
                }
                
                publisher.Publisher = textBoxPublish.Text.Trim();
                publisher.Address = textBoxAddress.Text.Trim();
                publisher.Tel = textBoxTel.Text.Trim();
                publisher.Fax = textBoxFax.Text.Trim();
                if (textBoxPostcode.Text.Length > 6)
                {
                    MessageBox.Show("邮编过长");
                    return;
                }
                publisher.Postcode = decimal.Parse(textBoxPostcode.Text);
                publisher.http = textBoxhttp.Text.Trim();
                int result = library.SaveChanges();
                String str = result == 1 ? "修改出版社成功" : "修改出版社失败";
                MessageBox.Show(str);
                this.DialogResult = DialogResult.OK;
                this.Close();

            }
            else
            {
                MessageBox.Show("有非空项");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            this.Close();
        }
    }
}
